import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/pop/normal_pop.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';

import 'default_bottom_sheet.dart';

/// 添加支付方式
showPaymentMethodBankBottomSheet(
  BuildContext context,
  SettingDataPaymentType paymentMethod,
  List<AccountEntity> accounts, {
  String amount,
  String hideKey,
}) async {
  SettingDataPaymentType _currentSelectCode = paymentMethod;
  final result = await showDefaultBottomSheet(
    context,
    child: PaymentMethodBankBottomSheet(
      value: paymentMethod,
      selectCallBack: (data) {
        _currentSelectCode = data;
      },
      hideKey: hideKey,
      accounts: accounts,
      amount: amount,
    ),
    title: S.current.payment_method,
    isScrollControlled: true,
  );
  if (result != null) {
    return result;
  }
  return _currentSelectCode;
}

class PaymentMethodBankBottomSheet extends StatefulWidget {
  final SettingDataPaymentType value;
  final ValueChanged<SettingDataPaymentType> selectCallBack;
  final List<AccountEntity> accounts;
  final String hideKey;
  final String amount;

  const PaymentMethodBankBottomSheet({
    Key key,
    this.value,
    this.amount,
    this.hideKey,
    this.accounts,
    this.selectCallBack,
  }) : super(key: key);

  @override
  _PaymentMethodBankBottomSheetState createState() =>
      _PaymentMethodBankBottomSheetState();
}

class _PaymentMethodBankBottomSheetState
    extends State<PaymentMethodBankBottomSheet> {
  List<SettingDataPaymentType> paymentMethodList = [];

  SettingDataPaymentType currentSelect;

  AccountEntity currentSelectBank;

  bool get enable => currentSelect != null && currentSelectBank != null;

  @override
  void initState() {
    super.initState();
    if (Config.settingData != null) {
      paymentMethodList =
          List<SettingDataPaymentType>.from(Config.settingData.paymentType);
      if (widget.hideKey != null) {
        paymentMethodList
            .removeWhere((element) => element.key == widget.hideKey);
      }
    }
    currentSelect = widget.value;
  }

  void onSelectBank(AccountEntity value) {
    if (currentSelectBank == value) return;
    setState(() {
      currentSelectBank = value;
    });
  }

  void onChangeSalesTrend(SettingDataPaymentType value) {
    if (currentSelect == value) return;
    setState(() {
      currentSelect = value;
    });
    widget.selectCallBack?.call(value);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        if (widget.amount == null)
          SizedBox(
            height: getDp(17),
            width: MediaQuery.of(context).size.width,
          )
        else
          Padding(
            padding: EdgeInsets.only(
                left: getDp(43), top: getDp(25), bottom: getDp(20)),
            child: Text(
              '${S.current.total}: ${widget.amount}',
              style: TextStyle(
                color: aceBlackColor,
                fontSize: getSp(16),
                fontWeight: FontWeight.w700,
                height: 22 / 16,
              ),
            ),
          ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: getDp(30),
          ),
          child: Wrap(
            alignment: WrapAlignment.start,
            crossAxisAlignment: WrapCrossAlignment.start,
            spacing: getDp(10),
            runSpacing: getDp(10),
            children: paymentMethodList
                .map((e) => PressedOpacityButton(
                      onPressed: () => onChangeSalesTrend(e),
                      padding: EdgeInsets.symmetric(
                        vertical: getDp(10),
                        horizontal: getDp(10),
                      ),
                      child: NormalRadio(
                        title: e.name,
                        isSelect: currentSelect == e,
                        minWidth:
                            (MediaQuery.of(context).size.width - getDp(150)) /
                                2,
                      ),
                    ))
                .toList(),
          ),
        ),
        AppSpaces.h20,
        NormalPop<AccountEntity>(
          select: currentSelectBank,
          group: widget.accounts,
          hint: S.current.select_bank_account,
          isTop: true,
          buildDisplayName: (e) => '${e.bankName} ${e.accountNumber ?? ''}',
          onSelect: onSelectBank,
        ),
        AppSpaces.h30,
        NormalButton(
          onTap: enable ? _onSave : null,
          title: S.current.save,
        ),
      ],
    );
  }

  void _onSave() {
    Navigator.of(context).pop({
      'account': currentSelectBank,
      'paymentType': currentSelect,
    });
  }
}
